
//
//  CPJNewFeatureView.swift
//  Sina
//
//  Created by YYY on 2017/8/11.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

class CPJNewFeatureView: UIView {

    @IBOutlet weak var scrollView: UIScrollView!
    
    @IBOutlet weak var enterButton: UIButton!
    
    @IBOutlet weak var pageControl: UIPageControl!
    
    @IBAction func enterAction() {
        removeFromSuperview()
    }
    
  
    //类方法
    class func newFeatureView() -> CPJNewFeatureView
    {
        let nib = UINib(nibName: "CPJNewFeatureView", bundle: nil)
        let view = nib.instantiate(withOwner: nil, options: nil)[0] as! CPJNewFeatureView
        view.frame = UIScreen.main.bounds
        return view
    }
    
    override func awakeFromNib() {
        let count = 4
        let rect = UIScreen.main.bounds
        
        for i in 0..<count
        {
            let imageName = "new_feature_\(i + 1)"
            
            let imageView = UIImageView(image: UIImage(named: imageName))
            imageView.frame = rect.offsetBy(dx: CGFloat(i) * kScreenWidth, dy: 0)
            scrollView.addSubview(imageView)
        }
        
        scrollView.contentSize = CGSize(width: kScreenWidth * CGFloat(count + 1), height: 0)
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        
        //设置代理
        scrollView.delegate = self
        //隐藏按钮
        self.enterButton.isHidden = true
    }
    
}
extension CPJNewFeatureView: UIScrollViewDelegate
{
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x/kScreenWidth)
        
        if page == scrollView.subviews.count
        {
            removeFromSuperview()
        }
        enterButton.isHidden = (page != scrollView.subviews.count-1)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        enterButton.isHidden = true
        let page = Int(scrollView.contentOffset.x/kScreenWidth + 0.5)
        pageControl.currentPage = page
        pageControl.isHidden = (page == scrollView.subviews.count)
    }
}
